//
//  HotZhongShuViewController.m
//  CarWorld
//
//  Created by yuyu on 15/3/30.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "HotZhongShuViewController.h"
#import "HotZhongShuCellTableViewCell.h"
#import "HotImageLabel.h"
#import "HotCarPictureViewController.h"
#import "HotZhongShuData.h"
#import "NSString+Attributed.h"
@interface HotZhongShuViewController ()
@property(nonatomic,retain)UIActivityIndicatorView *activityIndicator;
@property(nonatomic,retain)NSMutableArray *Datas;
@property(nonatomic,retain)HotImageLabel *imageLabel;
@end

@implementation HotZhongShuViewController

- (void)dealloc
{
    [_activityIndicator release];
    [_mid release];
    [_Datas release];
    [_imageLabel release];
    [super dealloc];
}

- (void)entePictureList
{
    HotCarPictureViewController *hotCartPicVC = [[HotCarPictureViewController alloc] init];
    UINavigationController *hotCarPicNaVC = [[UINavigationController alloc] initWithRootViewController:hotCartPicVC];
    [hotCarPicNaVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:hotCarPicNaVC animated:YES completion:nil];
    [hotCartPicVC release];
    [hotCarPicNaVC release];
}

- (void)getHotZongHeadInfo
{
    [self addJuHua];
    
    NSString *stringUrl = [NSString stringWithFormat:@"http://mobile.auto.sohu.com/quote/mobile/quotation/valid/carModel/carModelsIssell.do?modelId=%@",self.mid];
    NSURL *url = [NSURL URLWithString:stringUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[[[NSOperationQueue alloc] init] autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            [self parisHeadData:data];
        }
    }];
}

- (void)getHotZongTableInfo
{
    
    NSString *stringUrl = [NSString stringWithFormat:@"http://db.auto.sohu.com/epicjson/pics.sip?action=mtypes&m=%@",self.mid];
    NSURL *url = [NSURL URLWithString:stringUrl];
    NSLog(@"%@",stringUrl);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[[[NSOperationQueue alloc] init] autorelease] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            [self parisTableData:data];
        }
    }];
}

- (void)parisTableData:(NSData *)data
{
    if (data) {
        NSString *string = [[NSString alloc] initWithData:data encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
        data = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic[@"RESULT"]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
        });
    }
}


- (void)parisHeadData:(NSData *)data
{
    if (data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"RESULT"];
        HotZhongShuData *headData = [[HotZhongShuData alloc] init];
        [headData setValuesForKeysWithDictionary:array[0]];
        dispatch_async(dispatch_get_main_queue(), ^{
            _imageLabel.nameLbl.text = headData.mname;
            _imageLabel.priceLbl.text = [NSString stringWithFormat:@"指导价:%@",headData.price];
            _imageLabel.priceLbl.attributedText = [_imageLabel.priceLbl.text tintSubStringColorWithDictionary:@{@"指导价:":[UIColor grayColor],headData.price:[UIColor redColor]}];
            _imageLabel.refPriceLbl.text = [NSString stringWithFormat:@"报  价:%@",headData.refPrice];
            _imageLabel.refPriceLbl.attributedText = [_imageLabel.refPriceLbl.text tintSubStringColorWithDictionary:@{@"报  价:":[UIColor grayColor],headData.refPrice:[UIColor redColor]}];
            _imageLabel.image_View.image = [UIImage imageWithData:[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:headData.logo]] returningResponse:nil error:nil]];
            [self stopAnimating];
        });
    }
}

- (void)addJuHua
{
    self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];

    CGFloat pX = [UIScreen mainScreen].bounds.size.width/2-50;
    _activityIndicator.frame = CGRectMake(pX, 100, 100, 50);
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 20)];
    lbl.text = @"正在加载...";
    lbl.font = [UIFont systemFontOfSize:10];
    lbl.textAlignment = NSTextAlignmentCenter;
    [_activityIndicator addSubview:lbl];
    [lbl release];
    [_activityIndicator startAnimating];
    [self.view addSubview:_activityIndicator];
    self.view.userInteractionEnabled = NO;
}

- (void)stopAnimating
{
    [_activityIndicator stopAnimating];
    self.view.userInteractionEnabled = YES;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   self.Datas = [NSMutableArray array];
    //self.view.backgroundColor = [UIColor whiteColor];

    //[self getHotZongHeadInfo];
    [self getHotZongTableInfo];
    
    CGRect rect = self.tableView.frame;
    rect.origin.x=rect.origin.y=0;
    rect.size.height = 80;
    _imageLabel = [[HotImageLabel alloc] initWithFrame:rect];
    _imageLabel.image_View.backgroundColor = [UIColor greenColor];
    _imageLabel.image_View.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(entePictureList)];
    [_imageLabel.image_View addGestureRecognizer:tapGesture];
    [tapGesture release];
    self.tableView.tableHeaderView = _imageLabel;
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *keyString = @"HotDetail";
    HotZhongShuCellTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:keyString];
    if (cell==nil) {
        cell = [[HotZhongShuCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:keyString];
    }
    //HotZhongShuData *cellData = _Datas[indexPath.row];
    cell.textLabel.text = @"yuyu";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (void)clickedSectionView:(HotSectionView *)hotSectionView
{
    NSLog(@"%@",hotSectionView.keyButton.titleLabel.text);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect rect = self.tableView.frame;
    rect.origin.x=rect.origin.y=0;
    rect.size.height = 30;
    HotSectionView *sectonView = [[HotSectionView alloc] initWithFrame:rect];
    NSArray *titles = [NSArray arrayWithObjects:@"在售车款",@"停售车款", nil];
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:2];
    for (int i=0; i<titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [arr addObject:btn];
    }
    sectonView.buttons = arr;
    sectonView.delegate = self;
    
    
    return sectonView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
